package com.hex.ds.hdtp.core.app.data.contrast.service.impl;

import com.hex.ds.hdtp.core.app.common.enums.BasicTaskStatus;
import com.hex.ds.hdtp.core.app.common.page.PageData;
import com.hex.ds.hdtp.core.app.common.page.converter.IPageAndPageDtoConverter;
import com.hex.ds.hdtp.core.app.common.page.dto.PageDto;
import com.hex.ds.hdtp.core.app.data.contrast.converter.ICrossDbContrastRunLogConverter;
import com.hex.ds.hdtp.core.app.data.contrast.dto.request.CrossDbContrastRunLogRequestDto;
import com.hex.ds.hdtp.core.app.data.contrast.dto.response.CrossDbContrastRunLogResponseDto;
import com.hex.ds.hdtp.core.app.data.contrast.dto.response.CrossDbContrastTemplateResponseDto;
import com.hex.ds.hdtp.core.app.data.contrast.service.ICrossDbContrastRunLogService;
import com.hex.ds.hdtp.core.app.data.contrast.service.ICrossDbContrastTemplateService;
import com.hex.ds.hdtp.core.inf.common.page.Page;
import com.hex.ds.hdtp.core.inf.data.contrast.po.CrossDbContrastRunLogPo;
import com.hex.ds.hdtp.core.inf.data.contrast.repository.ICrossDbContrastRunLogRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Package: com.hex.ds.hdtp.core.app.data.contrast.service.impl
 * @ClassName CrossDbContrastRunLogService
 * @Description: 跨库比对记录-业务接口实现
 * @Author: wz.li
 * @Date 2023/7/28 17:56
 * @Version v2.0.2
 **/
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class CrossDbContrastRunLogService implements ICrossDbContrastRunLogService {

    private final ICrossDbContrastRunLogRepository crossDbContrastRunLogRepository;

    private final ICrossDbContrastRunLogConverter converter;

    private final IPageAndPageDtoConverter pageAndPageDtoConverter;

    private final ICrossDbContrastTemplateService crossDbContrastTemplateService;

    /*
     * @Method: queryListByPage <br>
     * @Param: [crossDbContrastRunLogRequestDto, pageDto] <br>
     * @Return: com.hex.ds.hdtp.core.app.common.page.PageData <br>
     * @Description：分页查询<br>
     * @Author： wz.li<br>
     * @Date： 2023/7/28 17:56 <br>
     * @Version： V2.0.2<br>
     */
    @Override
    public PageData queryListByPage(CrossDbContrastRunLogRequestDto crossDbContrastRunLogRequestDto, PageDto pageDto) {
        final Page<CrossDbContrastRunLogPo> pageResult = crossDbContrastRunLogRepository.queryListByPage(
                converter.toCrossDbContrastRunLogPo(crossDbContrastRunLogRequestDto), pageAndPageDtoConverter.toPage(pageDto));
        List<CrossDbContrastRunLogResponseDto> responseDtos = converter.toCrossDbContrastRunLogResponseDtoList(pageResult.getData());
        // 补充外部数据
        supplementaryExternalData(responseDtos);
        return new PageData(responseDtos, pageResult.getTotal());
    }

    @Override
    public List<CrossDbContrastRunLogResponseDto> queryByTaskIdStatus(String taskPkId, String status) {
        return converter.toCrossDbContrastRunLogResponseDtoList(crossDbContrastRunLogRepository.queryByTaskIdStatus(taskPkId, status));

    }

    /**
     * @Method converterIdToName
     * @Param
     * @param responseDtos
     * @Return java.util.List<com.hex.ds.hdtp.core.app.data.contrast.dto.response.CrossDbContrastRunLogResponseDto>
     * @Description 补充外部数据
     * @Author Wang zhihao
     * @Date 2023/8/2 14:42
     * @Version V1.0
     */
    private List<CrossDbContrastRunLogResponseDto> supplementaryExternalData(List<CrossDbContrastRunLogResponseDto> responseDtos) {
        for (CrossDbContrastRunLogResponseDto crossDbContrastRunLog : responseDtos) {
            CrossDbContrastTemplateResponseDto contrastTemplate = crossDbContrastTemplateService.queryById(crossDbContrastRunLog.getTemplatePkId());
            if (contrastTemplate == null) {
                crossDbContrastRunLog.setTemplateName("模板不存在");
                continue;
            }
            crossDbContrastRunLog.setTemplateName(contrastTemplate.getName());
            crossDbContrastRunLog.setContrastScene(contrastTemplate.getContrastScene());
            crossDbContrastRunLog.setCronExpressions((contrastTemplate.getCronExpressions() != null) ? contrastTemplate.getCronExpressions() : null );
        }
        return responseDtos;
    }

    /*
     * @Method: add <br>
     * @Param: [crossDbContrastRunLogRequestDto] <br>
     * @Return: void <br>
     * @Description：新增<br>
     * @Author： wz.li<br>
     * @Date： 2023/7/28 17:56 <br>
     * @Version： V2.0.2<br>
     */
    @Override
    public void add(CrossDbContrastRunLogRequestDto crossDbContrastRunLogRequestDto) {
        CrossDbContrastRunLogPo crossDbContrastRunLogPo = converter.toCrossDbContrastRunLogPo(crossDbContrastRunLogRequestDto);
        int count = crossDbContrastRunLogRepository.add(crossDbContrastRunLogPo);
        if (count != 1) {
            throw new RuntimeException("新增跨库对比记录失败");
        }
    }

    /**
     * @Method addBackPkId
     * @Param
     * @param crossDbContrastRunLogRequestDto
     * @Return java.lang.String
     * @Description 新增并返回pkId
     * @Author Wang zhihao
     * @Date 2023/7/31 17:32
     * @Version V1.0
     */
    @Override
    public String addWithBackPkId(CrossDbContrastRunLogRequestDto crossDbContrastRunLogRequestDto) {
        CrossDbContrastRunLogPo crossDbContrastRunLogPo = converter.toCrossDbContrastRunLogPo(crossDbContrastRunLogRequestDto);
        int count = crossDbContrastRunLogRepository.add(crossDbContrastRunLogPo);
        if (count != 1) {
            throw new RuntimeException("新增跨库对比记录失败");
        }
        return crossDbContrastRunLogPo.getPkId();
    }

    /**
     * @Method modifySuccessNum
     * @Param
     * @param pkId
     * @Return void
     * @Description 根据主键更新成功数量
     * @Author Wang zhihao
     * @Date 2023/8/1 10:14
     * @Version V1.0
     */
    @Override
    public void modifySuccessNum(String pkId) {
        crossDbContrastRunLogRepository.modifySuccessNum(pkId);
        crossDbContrastRunLogRepository.modifyStatus(pkId);
    }

    /**
     * @Method modifyFailNum
     * @Param
     * @param pkId
     * @Return void
     * @Description 根据主键更新失败数量
     * @Author Wang zhihao
     * @Date 2023/8/1 10:14
     * @Version V1.0
     */
    @Override
    public void modifyFailNum(String pkId) {
        crossDbContrastRunLogRepository.modifyFailNum(pkId);
        crossDbContrastRunLogRepository.modifyStatus(pkId);
    }

    /**
     * @Method modifyStopTask
     * @Param pkId
     * @Return void
     * @Description 停止比对任务
     * @Author gj.xu
     * @Date 2024/1/11 20:35
     * @Version V2.0
     */
    @Override
    public void modifyStopTask(String pkId) {
        crossDbContrastRunLogRepository.modifyStopTask(pkId, BasicTaskStatus.STOPPED.name());
    }

    /*
     * @Method: modifyById <br>
     * @Param: [crossDbContrastRunLogRequestDto] <br>
     * @Return: void <br>
     * @Description：根据主键更新<br>
     * @Author： wz.li<br>
     * @Date： 2023/7/28 17:57 <br>
     * @Version： V2.0.2<br>
     */
    @Override
    public void modifyById(CrossDbContrastRunLogRequestDto crossDbContrastRunLogRequestDto) {
        int count = crossDbContrastRunLogRepository.modifyById(converter.toCrossDbContrastRunLogPo(crossDbContrastRunLogRequestDto));
        if (count != 1) {
            throw new RuntimeException("更新跨库对比记录失败");
        }
    }

    /*
     * @Method: removeById <br>
     * @Param: [pkId] <br>
     * @Return: void <br>
     * @Description：根据主键删除<br>
     * @Author： wz.li<br>
     * @Date： 2023/7/28 17:57 <br>
     * @Version： V2.0.2<br>
     */
    @Override
    public void removeById(String pkId) {
        int count = crossDbContrastRunLogRepository.removeById(pkId);
        if (count != 1) {
            throw new RuntimeException("删除跨库对比记录失败");
        }
    }
    /*
     * @param pkId:
     * @return String
     * @author Liang Fuhao
     * @description 根据id查状态
     * @date 2024/1/15 11:03
     */
    public String queryStatusByPkId(String pkId){
        return crossDbContrastRunLogRepository.queryStatusByPkId(pkId);
    }
}
